﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using Telerik.Web.UI;

namespace BVVD_Website.ql_danh_muc
{
    public partial class frmMonhoc : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                grvMonhoc.DataSource = getData();
                grvMonhoc.DataBind();

            }
        }

        protected void grvMonhoc_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            grvMonhoc.DataSource = getData();
        }

        public List<LST_MONHOC> getData()
        {
            BVVDDataStore<LST_MONHOC> db = new BVVDDataStore<LST_MONHOC>();
            return db.getFromDB(new Hashtable());

        }

        protected void grvMonhoc_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;

            LST_MONHOC monhoc = new LST_MONHOC
            {
                
                MAMH = (insertedItem.FindControl("txtMAMH") as RadTextBox).Text,
                TENMH = (insertedItem.FindControl("txtTENMH") as RadTextBox).Text,
                GHICHU = (insertedItem.FindControl("txtGHICHU") as RadTextBox).Text,
                TRANGTHAI = (insertedItem.FindControl("rblTRANGTHAI") as RadioButtonList).SelectedValue
            };
            BVVDDataContext db = new BVVDDataContext();
            db.LST_MONHOCs.InsertOnSubmit(monhoc);
            try
            {
                db.SubmitChanges();
                rnAlert.Text = "Thêm mới danh mục thành công ";
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra \n" + ex.Message;

            }


            rnAlert.Show();
        }

        protected void grvMonhoc_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            BVVDDataContext db = new BVVDDataContext();
            GridEditableItem editedItem = e.Item as GridEditableItem;
            string recid = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["RECID"].ToString();

            LST_MONHOC monhoc = (from item in db.LST_MONHOCs
                                   where item.RECID.ToString().Equals(recid)
                                   select item).SingleOrDefault();
            
            monhoc.MAMH = (editedItem.FindControl("txtMAMH") as RadTextBox).Text;
            monhoc.TENMH = (editedItem.FindControl("txtTENMH") as RadTextBox).Text;
            monhoc.GHICHU = (editedItem.FindControl("txtGHICHU") as RadTextBox).Text;
            monhoc.TRANGTHAI = (editedItem.FindControl("rblTRANGTHAI") as RadioButtonList).SelectedValue;
            try
            {
                db.SubmitChanges();
                rnAlert.Text = "Cập nhật thành công môn học có mã : " + monhoc.MAMH;
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra. \n " + ex.Message;
            }

            rnAlert.Show();
        }

        protected void grvMonhoc_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridDataItem item = (GridDataItem)e.Item;
            string recid = item.OwnerTableView.DataKeyValues[item.ItemIndex]["RECID"].ToString();
            BVVDDataContext db = new BVVDDataContext();
            LST_MONHOC monhoc = (from dm in db.LST_MONHOCs
                                   where dm.RECID.ToString().Equals(recid)
                                   select dm).SingleOrDefault();
            db.LST_MONHOCs.DeleteOnSubmit(monhoc);
            try
            {
                db.SubmitChanges();
                rnAlert.Text = "Xóa thành công môn học có mã : " + monhoc.MAMH;
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra. \n " + ex.Message;
            }

            rnAlert.Show();
        }

        protected void grvMonhoc_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                GridEditFormItem editFormItem = e.Item as GridEditFormItem;
                GridDataItem parentItem = editFormItem.ParentItem;

                RadioButtonList rblTRANGTHAI = editFormItem.FindControl("rblTRANGTHAI") as RadioButtonList;

                if (e.Item is GridEditFormInsertItem || e.Item is GridDataInsertItem)
                {

                }
                else
                {

                    rblTRANGTHAI.SelectedValue = editFormItem["TRANGTHAI"].Text;

                }

            }
        }
    }
}